<template>
	<view>
		<view class="header">
			<text class="iconfont icon-xiangzuojiantou
" @click="back">编辑地址</text>
		</view>
		<view class="content">
			<view class="itemleft">
				<view><text>收货人</text></view>
				<view><text>联系电话</text></view>
				<view><text>所在地区</text></view>
				<view><text>详细地址</text></view>
			</view>
			<view class="itemright">
				<input type="text" v-model="name" />
				<input type="number" v-model="tel" />
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-db">
							<picker @change="bindPickerChange" :value="index" mode="region">
								<view class="uni-input">{{address}} <text style="text-align: end;" class="iconfont icon-youjiantou"></text></view>
							</picker>
						</view>
					</view>
				</view>
				<input type="text" v-model="xiangxiaddress" />
			</view>
		</view>
		<view class="last">
			<text>设置为默认地址</text>
			<view>
				<switch :checked=switchecked @change="switch1Change" />
			</view>
		</view>
		<view class="btn">
			<button type="warn" @click="save">保存</button>
		</view>
		<uni-popup ref="popup1" type="message">
			<uni-popup-message type="warn" message="手机号不能为空" :duration="2000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="popup2" type="message">
			<uni-popup-message type="warn" message="收货人不能为空" :duration="2000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="popup3" type="message">
			<uni-popup-message type="warn" message="请选择收货地址" :duration="2000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="popup4" type="message">
			<uni-popup-message type="success" message="添加成功" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import {reactive,toRefs,onMounted} from 'vue';
	import http from '../../utils/http.js'
	import store from '../../store/index.js'
	export default {
		setup(){
			let data=reactive({
				array: ['武侯区', '新都区'],
				index: 0,
				name:'',
				tel:'',
				address:'',
				xiangxiaddress:'',
				switchecked:true,
				isMoren:1,
				s_yonghuId:'',
				gaikuoAddress:'',
				back(){
					uni.redirectTo({
						url: '/pages/changeAddress/changeAddress'
					});
				},
				bindPickerChange: function(e) {
					console.log('picker发送选择改变，携带值为', e.detail.value)
					data.index = e.detail.value
					data.address=e.detail.value.join('-')
					console.log(data.index)
				},
				switch1Change(e){
					console.log('switch1 发生 change 事件，携带值为', e.detail.value)
					data.switchecked=e.detail.value
					if(data.switchecked){
						data.isMoren=1
					}else{
						data.isMoren=0
					}
				},
				save(){
					if(data.name==''){
						console.log('收获人不能为空')
						this.$refs.popup2.open('center')
						setTimeout(()=>{
							this.$refs.popup2.close()
						},1000)
					}
					else if(data.tel==''){
						console.log('手机号不能为空')
						this.$refs.popup1.open('center')
						setTimeout(()=>{
							this.$refs.popup1.close()
						},1000)
					}
					else if(data.address==''){
						console.log('请选择收货地址')
						this.$refs.popup3.open('center')
						setTimeout(()=>{
							this.$refs.popup3.close()
						},1000)
					}
					else{
						console.log('添加成功')
						this.$refs.popup4.open('center')
						setTimeout(()=>{
							this.$refs.popup4.close()
						},1000)
						// console.log(data.address)
						data.gaikuoAddress=data.address
						let a=data.address.split('-')
						let str=''
						a.forEach((v,i)=>{
							str+=v
						})
						console.log(data.isMoren)
						if(data.isMoren){
							http({
								url:'/appUpdateMoren',
								method:'post',
								
							}).then((res)=>{
								if(res.data.code==202){
									http({
										url:'/appAddShouhuodizhi',
										method:'post',
										data:{
											s_yonghuId:data.s_yonghuId,
											s_tel:data.tel,
											s_name:data.name,
											s_address:str,
											s_xiangxidizhi:data.xiangxiaddress,
											s_isMoren:data.isMoren,
											s_gaikuodizhi:data.gaikuoAddress
										}
									}).then((res)=>{
										console.log(res.data)
										if(res.data.code==202){
											setTimeout(()=>{
												uni.navigateTo({
													url:'../changeAddress/changeAddress'
												})
											},1000)
										}
									})
								}
							})
						}
						else{
							http({
								url:'/appAddShouhuodizhi',
								method:'post',
								data:{
									s_yonghuId:data.s_yonghuId,
									s_tel:data.tel,
									s_name:data.name,
									s_address:str,
									s_xiangxidizhi:data.xiangxiaddress,
									s_isMoren:data.isMoren,
									s_gaikuodizhi:data.gaikuoAddress
								}
							}).then((res)=>{
								console.log(res.data)
								if(res.data.code==202){
									setTimeout(()=>{
										uni.navigateTo({
											url:'../changeAddress/changeAddress'
										})
									},1000)
								}
							})
						}
						
					}
				}
			})
			onMounted(()=>{
				data.s_yonghuId=store.state.userID
				console.log(data.s_yonghuId)
			})
			return {...toRefs(data)}
		},
		data() {
			return {
			
			}
		},
		methods: {
			
		}
	}
</script>

<style lang="scss">
	@import url("~@/static/fonts/iconfont.css");
	.header{
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		border-bottom: 1rpx solid #efefef;
		display: flex;
		justify-content: space-between;
	}
	.content{
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		.itemleft{
			view{
				margin-top: 22rpx;
			}
		}
		.itemright{
			flex: 1;
			margin-left: 40rpx;
			input{
				margin-top: 20rpx;
			}
			.uni-list{
				margin-top: 20rpx;
			}
			>view{
				>view{
					display: flex;
					justify-content: space-between;
				}
			}
		}
	}
	.last{
		padding: 0rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		view{
			switch{
				transform: scale(0.7);
				margin-top: 8rpx;
			}
		}
	}
	.btn{
		text-align: center;
		margin-top: 40rpx;
		button{
			width: 370rpx;
			font-size: 16px;
		}
	}
</style>
